//
//  XWCommon.m
//  XWKitDemo
//
//  Created by xiaowuxiaowu on 16/4/17.
//  Copyright © 2016年 xiaowuxiaowu. All rights reserved.
//

#import "XWCommon.h"
#import "CommonCrypto/CommonDigest.h"
#import<CommonCrypto/CommonCryptor.h>
#import <CoreMedia/CoreMedia.h>
#import <WebKit/WebKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import "TPLoginViewController.h"
#import "UIAlertView+Block.h"
//#import "MineInfoViewController.h"
//#import "PerfectViewController.h"

@implementation XWCommon

/////////////////////
+(AppDelegate*)shareAppDelegate
{
    AppDelegate* delegate = nil;
    delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    return delegate;
}

// 正则判断手机号码地址格式
BOOL xw_isMobileNumber(NSString *mobileNum)
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[1278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

id un_getController(Class view){
   return xw_getController(NSStringFromClass(view), nil);
    
}

id xw_getController(NSString *identifier,NSString *title)
{
    UIStoryboard *storyBoard = nil;
    if (!title || title.length <= 0) {
        //应用程序的名称和版本号等信息都保存在mainBundle的一个字典中，用下面代码可以取出来。
        NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
        title = [infoDict objectForKey:@"UIMainStoryboardFile"];
    }
    
    storyBoard = [UIStoryboard storyboardWithName:title bundle:nil];
    
    if (storyBoard == NULL) {
        return NULL;
    }
    
    UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:identifier];
    return viewController;
}

//判断邮箱
BOOL  xw_isValidateEmail(NSString *email){
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


//加密md5
NSString *xw_md5(NSString *inPutText){

    const char *cStr = [inPutText UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];//CC_MD5_DIGEST_LENGTH
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);

    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}
#pragma mark 转UTF8
NSString* changeToUTF8(NSString *str){
    NSString * urls ;
    urls =  (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)str, NULL, NULL,  kCFStringEncodingUTF8 ));
    return urls;
}


//判断身份证
BOOL xw_chk18PaperId(NSString *sPaperId){

    
    //判断位数
    
    
    if ([sPaperId length] != 15 && [sPaperId length] != 18) {
        return NO;
    }
    NSString *carid = sPaperId;
    
    long lSumQT =0;
    
    //加权因子
    
    int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
    
    //校验码
    
    unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};
    
    
    
    //将15位身份证号转换成18位
    
    NSMutableString *mString = [NSMutableString stringWithString:sPaperId];
    
    if ([sPaperId length] == 15) {
        
        [mString insertString:@"19" atIndex:6];
        
        long p = 0;
        
        const char *pid = [mString UTF8String];
        
        for (int i=0; i<=16; i++)
            
        {
            
            p += (pid[i]-48) * R[i];
            
        }
        
        int o = p%11;
        
        NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
        
        [mString insertString:string_content atIndex:[mString length]];
        
        carid = mString;
        
    }
    
    //判断地区码
    
    NSString * sProvince = [carid substringToIndex:2];
    
    if (!xw_areaCode(sProvince)) {
        
        return NO;
        
    }
    
    //判断年月日是否有效
    
    
    
    //年份
    
    int strYear = [getStringWithRange(carid,6,4) intValue];
    
    //月份
    
    int strMonth = [getStringWithRange(carid,10,2) intValue];
    //[[self getStringWithRange:carid Value1:10 Value2:2] intValue];
    
    //日
    
    int strDay = [getStringWithRange(carid,12,2) intValue];
    //[[self getStringWithRange:carid Value1:12 Value2:2] intValue];
    
    
    
    NSTimeZone *localZone = [NSTimeZone localTimeZone];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    
    [dateFormatter setTimeZone:localZone];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];
    
    if (date == nil) {
        
        return NO;
        
    }
    
    const char *PaperId  = [carid UTF8String];
    
    //检验长度
    
    if( 18 != strlen(PaperId)) return -1;
    
    
    
    //校验数字
    
    for (int i=0; i<18; i++)
        
    {
        
        if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) )
            
        {
            
            return NO;
            
        }
        
    }
    
    //验证最末的校验码
    
    for (int i=0; i<=16; i++)
        
    {
        
        lSumQT += (PaperId[i]-48) * R[i];
        
    }
    
    if (sChecker[lSumQT%11] != PaperId[17] )
        
    {
        
        return NO;
        
    }
    
    return YES;
    
    
}

static NSString *getStringWithRange(NSString * str,int value1,NSInteger value2){
    return [str substringWithRange:NSMakeRange(value1,value2)];
}

/**
 
 * 功能:判断是否在地区码内
 
 * 参数:地区码
 
 */

static BOOL xw_areaCode(NSString *code){

    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    
    [dic setObject:@"北京" forKey:@"11"];
    
    [dic setObject:@"天津" forKey:@"12"];
    
    [dic setObject:@"河北" forKey:@"13"];
    
    [dic setObject:@"山西" forKey:@"14"];
    
    [dic setObject:@"内蒙古" forKey:@"15"];
    
    [dic setObject:@"辽宁" forKey:@"21"];
    
    [dic setObject:@"吉林" forKey:@"22"];
    
    [dic setObject:@"黑龙江" forKey:@"23"];
    
    [dic setObject:@"上海" forKey:@"31"];
    
    [dic setObject:@"江苏" forKey:@"32"];
    
    [dic setObject:@"浙江" forKey:@"33"];
    
    [dic setObject:@"安徽" forKey:@"34"];
    
    [dic setObject:@"福建" forKey:@"35"];
    
    [dic setObject:@"江西" forKey:@"36"];
    
    [dic setObject:@"山东" forKey:@"37"];
    
    [dic setObject:@"河南" forKey:@"41"];
    
    [dic setObject:@"湖北" forKey:@"42"];
    
    [dic setObject:@"湖南" forKey:@"43"];
    
    [dic setObject:@"广东" forKey:@"44"];
    
    [dic setObject:@"广西" forKey:@"45"];
    
    [dic setObject:@"海南" forKey:@"46"];
    
    [dic setObject:@"重庆" forKey:@"50"];
    
    [dic setObject:@"四川" forKey:@"51"];
    
    [dic setObject:@"贵州" forKey:@"52"];
    
    [dic setObject:@"云南" forKey:@"53"];
    
    [dic setObject:@"西藏" forKey:@"54"];
    
    [dic setObject:@"陕西" forKey:@"61"];
    
    [dic setObject:@"甘肃" forKey:@"62"];
    
    [dic setObject:@"青海" forKey:@"63"];
    
    [dic setObject:@"宁夏" forKey:@"64"];
    
    [dic setObject:@"新疆" forKey:@"65"];
    
    [dic setObject:@"台湾" forKey:@"71"];
    
    [dic setObject:@"香港" forKey:@"81"];
    
    [dic setObject:@"澳门" forKey:@"82"];
    
    [dic setObject:@"国外" forKey:@"91"];
    
    if ([dic objectForKey:code] == nil) {
        
        return NO;
        
    }
    
    return YES;
    
}


//判断空值
NSString *xw_getNULLString(NSObject*obj)
{
    if (obj == nil||obj==NULL) {
        return @"";
    }
    if([obj isKindOfClass:[NSNull class]])
    {
        return @"";
    }
    if ([obj isKindOfClass:[NSString class]]) {
        return (NSString*)obj;
    }
    else if([obj isKindOfClass:[NSNumber class]])
    {
        NSNumber* number = (NSNumber*)obj;
        return  [number stringValue];
    }
    return nil;
}

id xw_getNULLArray(NSObject*obj){
    
    
    if (obj == nil||obj==NULL) {
        return @[];
    }
    return obj;
}

//获取本地视频的缩略图
+(UIImage *)xw_getVideoImage:(id )videoURL
{
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[videoURL isKindOfClass:[NSURL class]] ? (NSURL *)videoURL: [NSURL fileURLWithPath:videoURL] options:nil];
    
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    
    return thumb;
    
}

// DES加密 ：用CCCrypt函数加密一下，然后用base64编码
+ (NSString *)encrypt:(NSString *)sText key:(NSString *)key
{
    NSData* encryptData = [sText dataUsingEncoding:NSUTF8StringEncoding];
    const void *dataIn = (const void *)[encryptData bytes];
    size_t dataInLength = [encryptData length];
    
    /*
     DES加密 ：用CCCrypt函数加密一下，然后用base64编码
     */
    
    size_t dataOutAvailable = (dataInLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    uint8_t *dataOut = malloc( dataOutAvailable * sizeof(uint8_t));
    size_t dataOutMoved = 0;
    
    memset((void *)dataOut, 0x0, dataOutAvailable);//将已开辟内存空间buffer的首 1 个字节的值设为值 0
    
    NSString *initIv = @"12345678";
    const void *vkey = (const void *) [key UTF8String];
    const void *iv = (const void *) [initIv UTF8String];
    
    //CCCrypt函数 加密
    CCCrypt(kCCEncrypt,//  加密
            kCCAlgorithmDES,//  加密根据哪个标准（des，3des，aes。。。。）
            kCCOptionPKCS7Padding,//  选项分组密码算法(des:对每块分组加一次密  3DES：对每块分组加三个不同的密)
            vkey,  //密钥
            kCCKeySizeDES,//   DES 密钥的大小（kCCKeySizeDES=8）
            iv, //  可选的初始矢量
            dataIn, // 数据的存储单元
            dataInLength,// 数据的大小
            (void *)dataOut,// 用于返回数据
            dataOutAvailable,
            &dataOutMoved);
    
    //编码 base64
    NSData *data = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved];
    NSString *result = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];//[GTMBase64 stringByEncodingData:data];
    
    return result;
}


//拨打电话
+(void)getPhoneNuber:(NSString *)phone view:(UIView *)view{
    
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
//    
//    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
//    WKWebView * callWebview = [[WKWebView alloc] init];
//    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
//    [view addSubview:callWebview];
    
}
+(void)getPhoneSheet:(void (^)(BOOL isCancel,NSInteger row))block{
 
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *a1 = [UIAlertAction actionWithTitle:@"校园短号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block(NO,0);
    }];
    UIAlertAction *a2 = [UIAlertAction actionWithTitle:@"手机长号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         block(NO,1);
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
         block(YES,0);
    }];
    if(IOS_VERSION >= 9.0){
        [a1 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
        [a2 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
         [cancel setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
        
    }
    
    [sheet addAction:a1];
    [sheet addAction:a2];
    [sheet addAction:cancel];
      [[XWCommon shareAppDelegate].window.rootViewController presentViewController:sheet animated:YES completion:nil];
    
}
+(NSMutableAttributedString *)LabelAttributedtext:(NSString*)text
{
    NSString *string = text;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    [attrString addAttribute:NSForegroundColorAttributeName
                       value:[UIColor redColor]
                       range:[string rangeOfString:@"*"]];
    return attrString;
}




+(UIViewController *)getViewController{
    return objc_getAssociatedObject(self, @selector(setViewController:));
}
+(void)setViewController:(UIViewController *)viewController{
    objc_setAssociatedObject(self, _cmd, viewController, OBJC_ASSOCIATION_RETAIN);
}



//得到星座的算法
+(NSString *)getAstroWithMonth:(NSInteger)m day:(NSInteger)d{
    
    NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    
    NSString *astroFormat = @"102123444543";
    
    NSString *result;
    
    if (m<1||m>12||d<1||d>31){
        
        return @"错误日期格式!";
        
    }
    
    if(m==2 && d>29)
        
    {
        
        return @"错误日期格式!!";
        
    }else if(m==4 || m==6 || m==9 || m==11) {
        
        if (d>30) {
            
            return @"错误日期格式!!!";
            
        }
        
    }
    
    result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];
    
    return [result stringByAppendingString:@"座"];
    
}

////本地保存数据
//+ (void)saveDataToLocal{
//    [[NSUserDefaults standardUserDefaults]setObject:[UserModelTools getUserDic:[UserDataModel shareUser]] forKey:@"userData"];
//}


+ (void)getImageVideoPathFromPHAsset:(PHAsset *)asset Complete:(ResultPath)result {
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"video"];

    [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    
    
    
    NSArray *assetResources = [PHAssetResource assetResourcesForAsset:asset];
    PHAssetResource *resource;
    if (asset.mediaType == PHAssetMediaTypeImage) {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.version = PHImageRequestOptionsVersionCurrent;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        options.synchronous = YES;
        
        NSString *fileName = @"text.jpg";
        NSString *PATH_MOVIE_FILE = [path stringByAppendingPathComponent:fileName];
        [[NSFileManager defaultManager] removeItemAtPath:PATH_MOVIE_FILE error:nil];
       
        [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                                  options:options
                                                            resultHandler:
                 ^(NSData *imageData,
                   NSString *dataUTI,
                   UIImageOrientation orientation,
                   NSDictionary *info) {
                    if([imageData writeToFile:PATH_MOVIE_FILE atomically:YES])
                     result(PATH_MOVIE_FILE, fileName);
                     
                 }];
        
     
        return;
    }
    
    for (PHAssetResource *assetRes in assetResources) {
        if (assetRes.type == PHAssetResourceTypePairedVideo ||
            assetRes.type == PHAssetResourceTypeVideo) {
            resource = assetRes;
        }
    }
    NSString *fileName = @"tempAssetVideo.mov";
    if (resource.originalFilename) {
        fileName = resource.originalFilename;
    
    }
    
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]enumeratorAtPath:path];
    NSString *PATH_MOVIE_FILE = [path stringByAppendingPathComponent:fileName];
    for (NSString *name in enumerator)
    {
        if([fileName isEqualToString:name]){
             result(PATH_MOVIE_FILE, fileName);
            return;
        }
        DLog(@"文件名--%@",name);
//        [[NSFileManager defaultManager]removeItemAtPath:PATH_MOVIE_FILE error:nil];
    }
    
    if (asset.mediaType == PHAssetMediaTypeVideo || asset.mediaSubtypes == PHAssetMediaSubtypePhotoLive) {
        PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
        options.version = PHImageRequestOptionsVersionCurrent;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        
        
        [[NSFileManager defaultManager] removeItemAtPath:PATH_MOVIE_FILE error:nil];
        [[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource
                                                                    toFile:[NSURL fileURLWithPath:PATH_MOVIE_FILE]
                                                                   options:nil
                                                         completionHandler:^(NSError * _Nullable error) {
                                                             if (error) {
                                                                 result(nil, nil);
                                                                 [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
                                                                     ;
                                                                 } title:@"请求失败，请检查手机容量是否足够大" message:nil cancelButtonName:@"确定" otherButtonTitles:nil, nil];
                                                                 
                                                                 
                                                             } else {
                                                                 result(PATH_MOVIE_FILE, fileName);
                                                             }
                                                         }];
    } else {
        result(nil, nil);
    }
}

+ (void)getImageFromPHAsset:(PHAsset *)asset Complete:(ResultPath)result {
    __block NSData *data;
    PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:asset] firstObject];
    if (asset.mediaType == PHAssetMediaTypeImage) {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.version = PHImageRequestOptionsVersionCurrent;
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
        options.synchronous = YES;
        
        NSString *fileName = @"text.jpg";
        NSString *PATH_MOVIE_FILE = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
        [[NSFileManager defaultManager] removeItemAtPath:PATH_MOVIE_FILE error:nil];
        [[PHAssetResourceManager defaultManager] writeDataForAssetResource:resource
                                                                    toFile:[NSURL fileURLWithPath:PATH_MOVIE_FILE]
                                                                   options:nil
                                                         completionHandler:^(NSError * _Nullable error) {
                                                             if (error) {
                                                                 result(nil, nil);
                                                             } else {
                                                                 result(PATH_MOVIE_FILE, fileName);
                                                             }
                                                         }];
        
        [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                          options:options
                                                    resultHandler:
         ^(NSData *imageData,
           NSString *dataUTI,
           UIImageOrientation orientation,
           NSDictionary *info) {
             data = [NSData dataWithData:imageData];
         }];
    }
    
  
}


+(void)saveAsset:(AVAsset *)asset orUrl:(NSURL *)url export:(NSString *)exportPath chooseVideoAlbumBlock:(void(^)(UIImage *image,NSString *path))block{
    
    
    if(url)
        asset = [AVAsset assetWithURL:url];
    //   NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
    //    NSString *exportPath = [NSString stringWithFormat:@"%@/%@.mp4",
    //                            [NSHomeDirectory() stringByAppendingString:@"/tmp"],
    //                            @"4"];
    //    NSFileManager *fileManage = [NSFileManager defaultManager];
    //    NSError *error;
    //    if(![fileManage removeItemAtPath:exportPath error:&error]){
    //        NSLog(@"Unable to move file: %@", [error localizedDescription]);
    //    }
    exportSession.outputURL = [NSURL fileURLWithPath:exportPath];
    NSLog(@"%@", exportPath);
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        
        switch ([exportSession status]) {
            case AVAssetExportSessionStatusFailed:{
                block(nil,exportPath);
                
                NSLog(@"Export failed: %@---%@", [[exportSession error] localizedDescription],[exportSession error]);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
                        ;
                    } title:@"提示" message:@"拍摄视频出错" cancelButtonName:@"确定" otherButtonTitles:nil, nil];
                });
            }
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"Export canceled");
                break;
            case AVAssetExportSessionStatusCompleted:{
                block([XWCommon xw_getVideoImage:exportPath],exportPath);
            }
                NSLog(@"转换成功");
                break;
            default:
                break;
        }
    }];
    
}





BOOL xw_getLogin(){
    
    if(![LoginModel shareLoginModel].isLogin){
        
        [[XWCommon shareAppDelegate].window.rootViewController presentViewController:[[UINavigationController alloc] initWithRootViewController:[TPLoginViewController new]] animated:YES completion:nil];
        return NO;
    }
    return YES;
    
}


@end
